#include "gtest/gtest.h"
#include "writer_buf.h"

using namespace simple;

TEST(writer_buf_test, writeByte) {
    WriterBuf writerBuf(STDOUT_FILENO);
    EXPECT_EQ(writerBuf.writeByte('a'), 1);
    EXPECT_EQ(writerBuf.writeByte('b'), 1);
    EXPECT_EQ(writerBuf.buffered(), 2);
    writerBuf.flush();
    EXPECT_EQ(writerBuf.buffered(), 0);
}

TEST(writer_buf_test, writeBytes) {
    WriterBuf writerBuf(STDOUT_FILENO);
    char bytes[] = "test";
    EXPECT_EQ(writerBuf.writeBytes(bytes, (int) sizeof(bytes)), sizeof(bytes));
    writerBuf.flush();
    EXPECT_EQ(writerBuf.buffered(), 0);
}

/**
 * 测试writeBytes, 缓冲区打满
 */
TEST(writer_buf_test, writeBytes2) {
    char bytes[] = "1234567890";
    WriterBuf writerBuf(STDOUT_FILENO, 3);

    EXPECT_EQ(writerBuf.writeBytes(bytes, (int) sizeof(bytes)), sizeof(bytes));
    writerBuf.flush();
    EXPECT_EQ(writerBuf.buffered(), 0);
}

TEST(writer_buf_test, writeBytes3) {
    WriterBuf writerBuf(STDOUT_FILENO);
    std::string str = "test";
    std::vector<char> bytes(str.begin(), str.end());
    EXPECT_EQ(writerBuf.writeBytes(bytes), bytes.size());
    writerBuf.flush();
    EXPECT_EQ(writerBuf.buffered(), 0);
}
